package com.ruoyi.project.system.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.CollegeStaff;
import com.ruoyi.project.system.domain.Member;
import com.ruoyi.project.system.domain.Student;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ICollegeStaffService;
import com.ruoyi.project.system.service.IMemberService;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学院教职员Controller
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@RestController
@RequestMapping("/system/collegeStaff")
public class CollegeStaffController extends BaseController
{
    @Autowired
    private ICollegeStaffService collegeStaffService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IMemberService memberService;

    /**
     * 查询学院教职员列表
     */
    @PreAuthorize("@ss.hasPermi('system:collegestaff:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollegeStaff collegeStaff)
    {
        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            collegeStaff.setCollegeId(sysUser.getDeptId());
        }
        startPage();
        List<CollegeStaff> list = collegeStaffService.selectCollegeStaffList(collegeStaff);
        return getDataTable(list);
    }

    /**
     * 导出学院教职员列表
     */
    @PreAuthorize("@ss.hasPermi('system:collegestaff:export')")
    @Log(title = "学院教职员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CollegeStaff collegeStaff)
    {
        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            collegeStaff.setCollegeId(sysUser.getDeptId());
        }
        List<CollegeStaff> list = collegeStaffService.selectCollegeStaffList(collegeStaff);
        ExcelUtil<CollegeStaff> util = new ExcelUtil<CollegeStaff>(CollegeStaff.class);
        return util.exportExcel(list, "staff");
    }

    /**
     * 获取学院教职员详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:collegestaff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(collegeStaffService.selectCollegeStaffById(id));
    }

    /**
     * 新增学院教职员
     */
    @PreAuthorize("@ss.hasPermi('system:collegestaff:add')")
    @Log(title = "学院教职员", businessType = BusinessType.INSERT)
    @PostMapping

    public AjaxResult add(@RequestBody CollegeStaff collegeStaff)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        collegeStaff.setCreateDt(new Date());
        return toAjax(collegeStaffService.insertCollegeStaff(collegeStaff,loginUser.getUser()));
    }

    /**
     * 修改学院教职员
     */
    @PreAuthorize("@ss.hasPermi('system:collegestaff:edit')")
    @Log(title = "学院教职员", businessType = BusinessType.UPDATE)
    @PutMapping

    public AjaxResult edit(@RequestBody CollegeStaff collegeStaff)
    {
        return toAjax(collegeStaffService.updateCollegeStaff(collegeStaff));
    }

    /**
     * 删除学院教职员
     */
    @PreAuthorize("@ss.hasPermi('system:collegestaff:remove')")
    @Log(title = "学院教职员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(collegeStaffService.deleteCollegeStaffByIds(ids));
    }


    @Log(title = "教师导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:collegestaff:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CollegeStaff> util = new ExcelUtil<CollegeStaff>(CollegeStaff.class);
        List<CollegeStaff> CollegeStaffList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        String message = collegeStaffService.importCollegeStaff(CollegeStaffList, updateSupport);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<CollegeStaff> util = new ExcelUtil<CollegeStaff>(CollegeStaff.class);
        return util.importTemplateExcel("教师导入模板");
    }


    @GetMapping("/restPwd")
    public AjaxResult restPwd(Integer id)
    {
        CollegeStaff collegeStaff = collegeStaffService.selectCollegeStaffById(id);

        Member member = memberService.selectMemberById(collegeStaff.getMemberId().intValue());

        //修改当前的会员表用户密码
        member.setPwd("123456");
        if(memberService.updateMember(member)>0){
            return AjaxResult.success();
        }

        return AjaxResult.error("未知错误");
    }


    @PostMapping("/updatePwd")
    public AjaxResult updatePwd(@RequestBody CollegeStaff collegeStaff)
    {

        Member member = memberService.selectMemberById(collegeStaff.getMemberId().intValue());

        //修改当前的会员表用户密码
        member.setPwd(collegeStaff.getName());
        if(memberService.updateMember(member)>0){
            return AjaxResult.success();
        }
        return AjaxResult.error("未知错误");
    }


}
